/**
 * 
 */
package com.yunyike.controller.cart;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.dto.cart.CartDTO;
import com.yunyike.exception.BadRequestException;
import com.yunyike.model.User;
import com.yunyike.response.Response;
import com.yunyike.service.ICartService;

/**
 * @author zhengqiang.shi
 * 2015年11月21日 下午6:15:28
 */
@Controller
@RequestMapping(value=RequestConstant.CART)
public class CartController extends BaseController{

	private static Logger log = LoggerFactory.getLogger(CartController.class);
	
	@Autowired
	private ICartService cartService;
	
	/**
	 * 获取购物车商品数量
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_QTY)
	public void qty(HttpServletRequest request, Integer isSelect, HttpServletResponse response){
		
		Response res = new Response();
		
		if(isSelect == null){
			isSelect = CommonConstant.NUMBER_0;
		}
		
		User user = getCurrentUser();
		
		int cartQty;
		try {
			cartQty = cartService.cartQty(user,isSelect == CommonConstant.NUMBER_0 ? false:true);
		} catch (Exception e) {
			log.error("cartService.cartQty has error:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		res.setData(cartQty);
		returnDatas(res, response);
	}
	
	/**
	 * 改变商品选中状态
	 * @param request
	 * @param merchandiseId
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_SELECT)
	public void select(HttpServletRequest request, String [] merchandiseId, HttpServletResponse response){
		
		log.info("cart.select, merchandiseId {}",Arrays.toString(merchandiseId));
		
		Response res = new Response();
		
		if(merchandiseId == null || merchandiseId.length < 1){
			res.getResult().setCode(CommonConstant.NUMBER__1);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__1));
			returnDatas(res, response);
			return;
		}
		
		User user = getCurrentUser();
		
		int result = 0;
		try {
			result = cartService.select(user, Arrays.asList(merchandiseId));
		} catch (BadRequestException be){
			log.error("cartService.select has error:"+be.getMessage(),be);
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		} finally {
			if(result != CommonConstant.NUMBER_1){
				log.error("cartService.select result is :{},please check.",result);
				res.getResult().setCode(CommonConstant.NUMBER__3);
				res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
				returnDatas(res, response);
				return;
			}
		} 
		
		returnDatas(res, response);
		
		return;
	}
	
	/**
	 * 添加购物车
	 * @param merchandiseId
	 * @param qty
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_ADD+"/{merchandiseId}/{qty}")
	public void add(@PathVariable String merchandiseId,@PathVariable Integer qty,
			HttpServletRequest request, HttpServletResponse response){
		
		Response res = new Response();
		
		// 当前用户
		User user = getCurrentUser();
		
		// 添加购物车
		int result;
		try {
			result = cartService.addCart(user, merchandiseId, qty);
		} catch (Exception e) {
			log.error("cartService.addCart has error:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		log.info("add cart end.result:{}",result);
		
		switch (result) {
		case CommonConstant.NUMBER_1:
			returnDatas(res, response);
			break;
		case CommonConstant.NUMBER_2:
			res.getResult().setCode(CommonConstant.NUMBER__5);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__5));
			returnDatas(res, response);
			break;
		}
	}
	
	/**
	 * 删除购物车
	 * @param merchandiseId
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_DELETE)
	public void delete(String [] merchandiseId,
			HttpServletRequest request, HttpServletResponse response){
		
		Response res = new Response();
		
		log.info("cart.delete ,merchandiseId:{}",Arrays.toString(merchandiseId));
		
		if(merchandiseId == null || merchandiseId.length < 1){
			res.getResult().setCode(CommonConstant.NUMBER__1);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__1));
			returnDatas(res, response);
			return;
		}
		
		User user = getCurrentUser();
		
		int result = 0;
		try {
			result = cartService.removeCart(user, Arrays.asList(merchandiseId));
		} catch (BadRequestException be){
			log.error("cartService.removeCart has error:"+be.getMessage(),be);
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		} finally {
			if(result != CommonConstant.NUMBER_1){
				log.error("cartService.removeCart result is :{},please check.",result);
				res.getResult().setCode(CommonConstant.NUMBER__3);
				res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
				returnDatas(res, response);
				return;
			}
		} 
		
		returnDatas(res, response);
		
		return;
	}
	
	/**
	 * 更新购物车
	 * @param merchandiseId
	 * @param qty
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_UPDATE+"/{merchandiseId}/{qty}")
	public void update(@PathVariable String merchandiseId,@PathVariable Integer qty,
			HttpServletRequest request, HttpServletResponse response){
		
		Response res = new Response();
		
		// 当前用户
		User user = getCurrentUser();
		
		// 更新购物车
		int result;
		try {
			result = cartService.updateCart(user, merchandiseId, qty);
		} catch (Exception e) {
			log.error("cartService.updateCart has error:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		log.info("updateCart cart end.result:{}",result);
		
		switch (result) {
		case CommonConstant.NUMBER_1:
			returnDatas(res, response);
			break;
		case CommonConstant.NUMBER_2:
			res.getResult().setCode(CommonConstant.NUMBER__5);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__5));
			returnDatas(res, response);
			break;
		}
	}
	
	/**
	 * 获取购物车信息
	 * @param request
	 * @param response
	 * @param model
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_INDEX)
	public void index(HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		Response res = new Response();
		
		// 获取当前用户
		User user = getCurrentUser();
		
		CartDTO cartDTO;
		try {
			// 获取购物车信息
			cartDTO = cartService.getCart(user);
		} catch (Exception e) {
			log.error("cartService.getCart has error:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		res.setData(cartDTO);
		returnDatas(res, response);
	}
	
	/**
	 * 结算
	 * @param request
	 * @param response
	 * @param model
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.CART_SETTLEMENT)
	public void settlement(HttpServletRequest request,String [] merchandiseId,
			HttpServletResponse response,Model model){
		
		Response res = new Response();
		
		log.info("cart.settlement ,merchandiseId:{}",Arrays.toString(merchandiseId));
		
		if(merchandiseId == null || merchandiseId.length < 1){
			res.getResult().setCode(CommonConstant.NUMBER__1);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__1));
			returnDatas(res, response);
			return;
		}
		
		// 获取当前用户
		User user = getCurrentUser();
		
		CartDTO cartDTO;
		try {
			// 获取结算信息
			cartDTO = cartService.settlement(user, Arrays.asList(merchandiseId));
		} catch (Exception e) {
			log.error("cartService.settlement has error:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		
		if(cartDTO == null){
			log.error("cartService.settlement return null");
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(cartDTO);
		returnDatas(res, response);
	}
}
